package top.lishuoboy.springmvc.a5;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 五、域对象共享数据
 */
@Controller
@RequestMapping("/a5")
@Slf4j
public class ScopeController {

    /**
     * 1、使用ServletAPI向request域对象共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/1
    @RequestMapping("/1")
    public String testServletAPI(HttpServletRequest request) {
        request.setAttribute("scope1", "hello,servletAPI");
        return "a5/scope";
    }

    /**
     * 2、使用ModelAndView向request域对象共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/2
    @RequestMapping("/2")
    public ModelAndView testModelAndView() {
        /** ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图，实现页面跳转
         */
        ModelAndView mv = new ModelAndView();
        //向请求域共享数据
        mv.addObject("scope2", "hello,ModelAndView");
        //设置视图，实现页面跳转
        mv.setViewName("a5/scope");
        return mv;
    }

    /**
     * 3、使用Model向request域对象共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/3
    @RequestMapping("/3")
    public String testModelAndView1(Model model) {
        model.addAttribute("scope3", "hello,Model");
        return "a5/scope";
    }

    /**
     * 4、使用map向request域对象共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/4
    @RequestMapping("/4")
    public String testMap(Map<String, Object> map) {
        map.put("scope4", "hello,Map");
        return "a5/scope";
    }

    /**
     * 5、使用ModelMap向request域对象共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/5
    @RequestMapping("/5")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("scope5", "hello,ModelMap");
        return "a5/scope";
    }

    /**
     * 6、Model、ModelMap、Map的关系
     * Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
     * public interface Model{}
     * public class ModelMap extends LinkedHashMap<String, Object> {}
     * public class ExtendedModelMap extends ModelMap implements Model {}
     * public class BindingAwareModelMap extends ExtendedModelMap {}
     */

    /**
     * 7、向session域共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/7
    @RequestMapping("/7")
    public String testSession(HttpSession session) {

        session.setAttribute("sessionScope", "hello,session");
        return "a5/scope";
    }

    /**
     * 8、向application域共享数据
     */
    // http://localhost:8080/lishuoboy_springmvc_war_exploded/a5/8
    @RequestMapping("/8")
    public String testApplication(HttpSession session) {
        ServletContext application = session.getServletContext();
        application.setAttribute("applicationScope", "hello,application");
        return "a5/scope";
    }


}
